<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-if="BUTTONS.add" type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增 </el-button>
        <el-button v-if="BUTTONS.import" type="primary" :icon="Upload" plain @click="batchAdd">导入数据</el-button>
        <el-button v-if="BUTTONS.export" type="primary" :icon="Download" plain @click="downloadFile">导出数据</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="View" @click="openDrawer('查看', scope.row)">查看</el-button>
        <el-button v-if="BUTTONS.edit" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">编辑</el-button>
        <el-button v-if="BUTTONS.delete" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <DetailDrawer ref="drawerRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive } from "vue";
import { ExportObj, Server } from "@/api/types/base";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import { ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import ImportExcel from "@/components/ImportExcel/index.vue";
import DetailDrawer from "./detailDrawer.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen, Download, View, Upload } from "@element-plus/icons-vue";
import apiService from "@/api/services/apiService";
import { useDateFormat } from "@vueuse/core";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { ExportColType } from "@/enums/sysEnum";

const initVar = reactive({
  tbName: "server",
  tbCnName: "服务器"
});

// ProTable 实例
const proTable = ref<ProTableInstance>();
const { BUTTONS } = useAuthButtons();

const initParam = reactive({
  // user_id: userStore.userInfo._id
});

const getTableList = (params: any) => {
  return apiService.findPage(initVar.tbName, params.condition, params.options);
};

// 表格配置项
const columns = reactive<ColumnProps<Server>[]>([
  { type: "index", label: "序号", width: 85 },
  { prop: "name", label: "服务名称", width: 160 },
  { prop: "system", label: "操作系统", width: 180, search: { el: "input", like: true } },
  { prop: "ip", label: "IP地址", width: 120, search: { el: "input", like: true } },
  { prop: "use", label: "用途", width: 200, align: "left", search: { el: "input", like: true } },
  { prop: "cpu", label: "CPU(核)", width: 85 },
  { prop: "ram", label: "内存(G)", width: 85 },
  { prop: "hdd", label: "硬盘(G)", width: 85 },
  { prop: "resouceState", label: "使用状态", width: 100, search: { el: "input", like: true } },
  { prop: "address", label: "服务器类型", width: 100 },
  { prop: "ownerUser", label: "负责人", width: 100, search: { el: "input", like: true } },
  { prop: "useDate", label: "使用期限", width: 100 },
  {
    prop: "firewall",
    label: "防火墙",
    width: 85,
    search: { el: "select", props: { filterable: true } },
    enum: () => apiService.findClassify("IS_NOT"),
    fieldNames: { label: "name", value: "value" }
  },
  { prop: "level", label: "重要级别", width: 100 },
  { prop: "soft", label: "安装的软件", width: 200 },
  { prop: "operation", label: "操作", align: "left", fixed: "right", width: 240 }
]);

// 删除信息
const deleteAccount = async (params: Server) => {
  await useHandleData(apiService.deleteById, initVar.tbName, params._id, `删除【${params.ip}】`);
  proTable.value?.getTableList();
};

const getExcelConfig = (): ExportObj => {
  return {
    params: proTable.value?.totalParam,
    tableName: initVar.tbName,
    keyPropName: "ip",
    enumTypes: [],
    tableConfigs: [],
    header: [
      { header: "序号", width: 6, ext: { prop: "index", type: ExportColType.INDEX } },
      { header: "服务名称", width: 16, ext: { prop: "name", type: ExportColType.DEFAULT } },
      { header: "操作系统", width: 16, ext: { prop: "system", type: ExportColType.DEFAULT } },
      { header: "IP地址", width: 10, ext: { prop: "ip", type: ExportColType.DEFAULT } },
      { header: "用途", width: 40, ext: { prop: "use", type: ExportColType.DEFAULT } },
      { header: "CPU(核)", width: 8, ext: { prop: "cpu", type: ExportColType.DEFAULT } },
      { header: "内存(G)", width: 8, ext: { prop: "ram", type: ExportColType.DEFAULT } },
      { header: "硬盘(G)", width: 8, ext: { prop: "hdd", type: ExportColType.DEFAULT } },
      { header: "使用状态", width: 8, ext: { prop: "resouceState", type: ExportColType.DEFAULT } },
      { header: "服务器类型", width: 8, ext: { prop: "address", type: ExportColType.DEFAULT } },
      { header: "负责人", width: 8, ext: { prop: "ownerUser", type: ExportColType.DEFAULT } },
      { header: "使用期限", width: 8, ext: { prop: "useDate", type: ExportColType.DEFAULT } },
      { header: "重要级别", width: 8, ext: { prop: "level", type: ExportColType.DEFAULT } },
      { header: "安装的软件", width: 30, ext: { prop: "soft", type: ExportColType.DEFAULT } },
      { header: "用户名", width: 12, ext: { prop: "account", type: ExportColType.DEFAULT } }
      // { header: "密码", width: 12, ext: { prop: "pwd", type: ExportColType.DEFAULT } }
    ]
  };
};

// 导出数据
const downloadFile = async () => {
  ElMessageBox.confirm("确认导出数据?", "温馨提示", { type: "warning" }).then(() =>
    useDownload(
      apiService.commonExport,
      `服务器清单-${useDateFormat(new Date(), "YYYY-MM-DD").value}`,
      "",
      getExcelConfig(),
      false
    )
  );
};

// 导入数据
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null);
const batchAdd = () => {
  const params = {
    title: "服务器",
    defaultTip: "服务器IP已有则跳过",
    coverTip: "服务器IP相同则覆盖",
    tempUrl: "/template/server",
    getTableList: proTable.value?.getTableList,
    importApi: apiService.commonImport,
    config: getExcelConfig()
  };
  dialogRef.value?.acceptParams(params);
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof DetailDrawer> | null>(null);
const openDrawer = (title: string, row: Partial<Server> = {}) => {
  const params = {
    title,
    tbName: initVar.tbName,
    isView: title === "查看",
    row: row,
    api: title.indexOf("新增") !== -1 ? apiService.save : title === "编辑" ? apiService.updateById : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
